var repastVm=new Vue({
	el: '#repast-manager',
  	beforeCompile:function(){
		$("body").LoadingOverlay("show",{
	  		fade : [500, 200]
		});
  	},
  	ready:function(){
  		//处理页码
  		this.page=laypage.handlePage();
  		//填充数据
  		this.handleContent();
  		//渲染菜单
  		this.loadMenu();
  	},
  	data:{
  		page:0,
  		size:10,
  		sort:"created,desc",
  		searchName:null,
  		searchType:'',
  		searchState:'',
  		info:{
  			rece:'',
  			actual:'',
  			descr:''
  		},
  		menu:[],
  		prtmenu:[],
  		selInfo:{},
  		currSels:'',
  		initLaypage:false,
  		content:null,
  		dicType:{"01":"普通","02":"包间"}
  	},
  	methods:{
  		createRecord:function(info){
  			this.$http.post("repast/oper",{tbid:info['ID']}).then((response) => {
  				info['REPASTID']=response.data;
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		editRecord:function(info){
  			//this.$http.get("repast/total",{params:{repastId:info['REPASTID']}}).then((response) => {
  			this.$http.get("menu/repast",{params:{repastId:info['REPASTID']}}).then((response) => {
  				this.selInfo=info;
  				this.prtmenu=response.data;
  				var total = this.calcTotal(this.prtmenu,'price','num');
  				this.info['id']=info['REPASTID'];
  				this.info['rece']=total;
  				this.info['actual']=total;
  				$("#repast-modal").modal("show");
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		saveRecord:function(){
			this.$http.put("repast/oper",this.info).then((response) => {
				this.selInfo['REPASTID']=null;
				$("#repast-modal").modal("hide");
				$.alt.confirm("提示","是否确认打印账单？",function(){
					$("#print-menu").printArea();
	  				return true;
	  			});
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		operMenu:function(info){
  			this.$http.get("menu/repast",{params:{repastId:info['REPASTID']}}).then((response) => {
  				this.selInfo=info;
  				$("#menu-sels").select2("val","").trigger("change");
  				this.menu=response.data;
  				$("#repast-menu-modal").modal("show");
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		addMenuRecord:function(){
  			var params=new Array();
  			_.each(this.currSels.split(","),function(value){
  				params.push({menuid:value,repastid:this.selInfo['REPASTID']});
  			}.bind(this));
  			this.$http.post("repast/menu/oper",params).then((response) => {
  				$("#menu-sels").select2("val","").trigger("change");
  				this.menu=response.data;
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		saveMenuReocrd:function(info){
  			this.$http.put("repast/menu/oper",{id:info.mid,num:info.newnum}).then((response) => {
  				info.num=info.newnum;
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		delMenuRecord:function(id){
  			$.alt.confirm("提示","您确认要删除该记录吗？",function(){
  				this.$http.delete("repast/menu/oper",{params:{id:id}}).then((response) => {
  					this.menu=_.reject(this.menu,{mid:id});
		  		}, (response) => {
		  			$.alt.message("提示",response.body);
		  		});
  				return true;
  			}.bind(this));
  		},
  		reloadContent:function(page){
  			$("body").LoadingOverlay("show",{
		  		fade : [500, 200]
			});
  			this.page=page;
			this.initLaypage=false;
			this.handleContent();
  		},
  		handleContent:function(){
  			this.$http.post("table/list",{
  				page:this.page,
  				size:this.size,
  				sort:this.sort,
  				searchName:this.searchName,
  				searchType:this.searchType,
  				searchState:this.searchState
	  		},{emulateJSON : true}).then((response) => {
	  			this.content=response.data['content'];
	  			if(!this.initLaypage){
		  			//分页初始化
		  			laypage({
		  			    cont: 'laypage-ctl',
		  			    skin: 'molv',
		  			    pages: response.data['totalPages'],
		  			    curr: this.page+1,
		  			    //hash: 'page',
		  			    jump: function(o){
		  			    	if(this.initLaypage){
		  			    		$("body").LoadingOverlay("show",{
		  					  		fade : [300, 200]
		  						});
		  			    		this.page=o.curr-1;
			  			    	this.handleContent();
		  			    	}
		  			    }.bind(this)
		  			});
		  			this.initLaypage=true;
	  			}
	  			$("body").LoadingOverlay("hide", true);
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		loadMenu:function(){
  			this.$http.get("menu/all").then((response) => {
  				$("#menu-sels").select2({
  					multiple:true,
  					data:{ results: response.data, text: 'name' },
  	  			    formatResult: function(item){
  	  			    	return item.name.concat("-").concat(item.code);
  	  			    },
	  	  			matcher: function(term, text) {
		  	  			if(term==""){
		  	  				return true;
		  	  			}
		  	  			return text.toUpperCase().indexOf(term.toUpperCase())>-1 || pinyin.getCamelChars(text).toUpperCase().indexOf(term.toUpperCase())>-1
		  	  		},
  	  			    placeholder:"输入编码|简拼即可检索"
  				});
	  		}, (response) => {
	  			$.alt.message("提示",response.body);
	  		});
  		},
  		calcTotal:function(array,price,num){
  			var total=0;
  			_.each(array,function(value){
  				total+=value[price]*value[num];
  			});
  			return total.toFixed(2);
  		}
  	}
});